package com.npave.zryhadapter.controller.web;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Controller
@RequestMapping(value = {"/wxmini"})
public class WXMiniController {
    @Autowired
    private WxMaService wxMaService;

    @GetMapping("/index")
    public void jumpIndex(HttpServletResponse response) throws WxErrorException, IOException {

        WxMaGenerateSchemeRequest.JumpWxa jumpWxa = WxMaGenerateSchemeRequest.JumpWxa.newBuilder()
                .path("pages/index/index").build();
        WxMaGenerateSchemeRequest request = WxMaGenerateSchemeRequest.newBuilder()
                .jumpWxa(jumpWxa)
                .build();
        String url = wxMaService.switchoverTo("wxa05a50bd61323753").getWxMaSchemeService().generate(request);
        log.info("跳转连接:{}",url);
        response.sendRedirect(url);
    }

    @GetMapping("/{page}/{info}")
    public void jumpWx(@PathVariable("page") String page,
                         @PathVariable("info") String info, HttpServletResponse response) throws WxErrorException, IOException {

        WxMaGenerateSchemeRequest.JumpWxa jumpWxa = WxMaGenerateSchemeRequest.JumpWxa.newBuilder()
                .path("pages/index/index").build();
        WxMaGenerateSchemeRequest request = WxMaGenerateSchemeRequest.newBuilder()
                .jumpWxa(jumpWxa)
                .build();
        String url = wxMaService.switchoverTo("wxa05a50bd61323753").getWxMaSchemeService().generate(request);
        log.info("跳转连接:{}",url);
        response.sendRedirect(url);
    }

}
